<template>
  <el-dialog
    class="add-enumerate-comp"
    title="规格设置"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    :visible="visible"
    :width="$setRem(800)"
    :before-close="handleCancel">
    <div class="btn-container">
      <el-button type="primary" size="medium" @click="handleAdd">新建</el-button>
      <el-button type="danger" size="medium" @click="handleDelete">删除</el-button>
    </div>
    <el-table
      class="table-container"
      :data="tableData"
      v-loading="tableLoading"
      element-loading-custom-class="custom-load"
      @selection-change="handleSelectionChange"
      row-key="id">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="序号" type="index" width="70" align="center"></el-table-column>
      <el-table-column label="规格名称" prop="modelName" align="center"></el-table-column>
      <el-table-column label="备注" prop="remark" align="center"></el-table-column>
<!--      <el-table-column label="操作" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          <span class="btn" @click="handleEdit(scope.row)">编辑</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <div class="empty-content" slot="empty">
        <i class="iconfont iconwushuju"></i>
        <p>暂无数据</p>
      </div>
    </el-table>
    <el-dialog
      class="add-model-comp"
      title="新增规格"
      :close-on-click-modal="false"
      :destroy-on-close="true"
      append-to-body
      :visible="addVisible"
      :width="$setRem(600)"
      :before-close="beforeClose">
      <el-form
        :model="ruleForm"
        :rules="rules"
        label-suffix="："
        size="medium"
        ref="ruleForm"
        :label-width="$setRem(140)"
        class="rule-form"
      >
        <el-row>
          <el-col :span="20">
            <el-form-item label="规格/型号" prop="modelName" required>
              <el-input
                type="text"
                placeholder="请输入规格/型号"
                v-model="ruleForm.modelName">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                :rows="4"
                placeholder="请输入备注"
                v-model="ruleForm.remark">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          size="medium"
          type="primary"
          :loading="loading"
          @click="handleSubmit">
          确 认
        </el-button>
        <el-button size="medium" @click="addVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <span slot="footer" class="dialog-footer">
      <el-button
        size="medium"
        type="primary"
        :loading="loading"
        @click="hanldeSave">
        确 认
      </el-button>
      <el-button size="medium" @click="handleCancel">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    options: {
      type: Object,
      default: () => ({})
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      tableData: [],
      cacheData: [],
      tableLoading: false,
      multipleSelection: [],
      addVisible: false,
      currentData: {},
      ruleForm: {
        modelName: '',
        materialTypeId: '',
        remark: ''
      },
      rules: {
        modelName: [
          { required: true, message: '请输入规格/型号' },
        ],
      },
    };
  },
  watch: {
    options: {
      deep: true,
      handler(data) {
        if (data && data.id) {
          this.getTableData();
          this.ruleForm.materialTypeId = data.id;
        }
      }
    },
    visible(val) {
      if (!val) {
        this.tableData = [];
        this.cacheData = [];
      }
    }
  },
  methods: {
    /**
     * 获取列表数据
     */
    async getTableData() {
      this.tableLoading = true;
      const params = {
        id: this.options.id
      };
      const { success, data: {
        data
      }} = await this.$Model.getStockModelList(params);
      this.tableLoading = false;
      if (success && data) {
        this.tableData = data || [];
        this.cacheData = data;
      }
    },
    // 新建
    handleAdd() {
      this.ruleForm = {
        modelName: '',
        materialTypeId: this.options.id,
        remark: '',
        idFlag: this.$getRandomNum()
      };
      this.addVisible = true;
    },
    handleDelete() {
      if (!this.multipleSelection.length) {
        return this.$message.warning('请勾选选项');
      }
      this.tableData = this.tableData.filter(item => {
        const itemId = item.id || item.idFlag;
        return !this.multipleSelection.some(v => {
          return (v.id || v.idFlag) === itemId;
        });
      });
      this.multipleSelection = [];
    },
    // 清除参数
    clear() {
      this.tableLoading = false;
      this.tableData = [];
      this.multipleSelection = [];
    },
    // 选择
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    beforeClose() {
      this.addVisible = false;
      this.currentData = {};
    },
    handleSubmit() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          this.tableData.unshift({ ...this.ruleForm });
          this.addVisible = false;
        }
      });
    },
    // 取消
    handleCancel() {
      this.$emit('cancel');
    },
    // 保存
    hanldeSave() {
      const addData = this.tableData.filter(v => !v.id);
      const deleteData = this.cacheData.filter(v => {
        return !this.tableData.some(item => item.id === v.id);
      });
      this.$emit('submit', {
        addData,
        deleteData,
        cacheData: this.cacheData
      });
    }
  }
};
</script>

<style scoped lang="scss">
  @import '../../../assets/styles/dialog';
  .btn-container {
    margin: 10px 0;
    text-align: right;
  }
</style>
